package org.jbp.timelapse;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PictureLogger
{
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd @ HH:mm:ss.SSS");
	
	private static PictureLogger instance;
	
	private PrintStream ps;
	
	private PictureLogger()
	{
		//EMPTY
	}
	
	public static PictureLogger getInstance()
	{
		if(instance == null)
		{
			instance = new PictureLogger();
		}
		return instance;
	}
	
	public void logStart(int nPictures, int interval, int initialDelay, long startTimestamp)
	{
		if(ps != null)
		{
			try
			{
				File logFile = new File(TimeLapseConstants.DEFAULT_DIR+startTimestamp+"/session-"+startTimestamp+".log");
				logFile.getParentFile().mkdirs();
				logFile.createNewFile();
				ps = new PrintStream(logFile);
				Date startDate = new Date(startTimestamp);
				ps.println(dateFormat.format(startDate)+"\tStarted taking\n\t"+nPictures+" pictures, at\n\t"+interval+"ms interval, with\n\t"+initialDelay+"ms initial delay");
			}
			catch(FileNotFoundException e)
			{
				ps = null;
				//TODO report
			}
			catch(IOException e)
			{
				ps = null;
				//TODO report
			}
		}
	}
	
	public void logPicture(TimeLapsePictureData pictureData)
	{
		if(ps != null && pictureData != null)
		{
			Date date = new Date(pictureData.getTimestamp());
			ps.println(dateFormat.format(date)+"\t picture "+pictureData.getName()+" taken.");
		}
	}
}